cmake_minimum_required(VERSION 3.8)
project(rikibot_foc_driver)

# 默认使用C++17标准
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# 查找依赖包
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(nav_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(tf2_ros REQUIRED)
find_package(tf2_geometry_msgs REQUIRED)

# 检测系统架构
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm")
    set(ARCH_SPECIFIC_LIB_DIRS
        /usr/lib/aarch64-linux-gnu
        /usr/lib/arm-linux-gnueabihf
    )
    message(STATUS "Detected ARM architecture")
else()
    set(ARCH_SPECIFIC_LIB_DIRS
        /usr/lib/x86_64-linux-gnu
    )
    message(STATUS "Detected x86 architecture")
endif()

# 尝试查找 libserial 的 CMake 配置文件
find_package(libserial QUIET)

if(NOT libserial_FOUND)
    message(STATUS "libserial CMake config not found, using manual search")
    
    # 尝试在标准位置查找头文件
    find_path(LIBSERIAL_INCLUDE_DIRS
        NAMES SerialStream.h
        PATHS
            /usr/include
            /usr/local/include
        PATH_SUFFIXES libserial
    )
    
    # 尝试在架构特定位置查找库
    find_library(LIBSERIAL_LIBRARIES
        NAMES serial
        PATHS
            ${ARCH_SPECIFIC_LIB_DIRS}
            /usr/lib
            /usr/local/lib
    )
    
    if(NOT LIBSERIAL_INCLUDE_DIRS OR NOT LIBSERIAL_LIBRARIES)
        message(FATAL_ERROR "Failed to find libserial. Please ensure libserial-dev is installed.")
    else()
        message(STATUS "Found libserial headers: ${LIBSERIAL_INCLUDE_DIRS}")
        message(STATUS "Found libserial library: ${LIBSERIAL_LIBRARIES}")
        add_library(libserial::serial INTERFACE IMPORTED)
        set_target_properties(libserial::serial PROPERTIES
            INTERFACE_INCLUDE_DIRECTORIES "${LIBSERIAL_INCLUDE_DIRS}"
            INTERFACE_LINK_LIBRARIES "${LIBSERIAL_LIBRARIES}"
        )
    endif()
else()
    message(STATUS "Found libserial via CMake config")
endif()

# 包含目录
include_directories(
  include
)

# 添加可执行文件
add_executable(rikibot_driver
  src/rikibot_driver.cpp
)

# 链接库
target_link_libraries(rikibot_driver
  ${rclcpp_LIBRARIES}
  ${nav_msgs_LIBRARIES}
  ${sensor_msgs_LIBRARIES}
  ${geometry_msgs_LIBRARIES}
  ${tf2_ros_LIBRARIES}
)

# 根据查找方式链接 libserial
if(TARGET libserial::serial)
    target_link_libraries(rikibot_driver libserial::serial)
else()
    target_link_libraries(rikibot_driver ${LIBSERIAL_LIBRARIES})
    target_include_directories(rikibot_driver PRIVATE ${LIBSERIAL_INCLUDE_DIRS})
endif()

# 添加依赖
ament_target_dependencies(rikibot_driver
  rclcpp
  nav_msgs
  sensor_msgs
  geometry_msgs
  tf2_ros
  tf2_geometry_msgs
)

# 安装目标
install(TARGETS rikibot_driver
  DESTINATION lib/${PROJECT_NAME}
)

# 安装launch文件
install(DIRECTORY
  launch
  rviz
  DESTINATION share/${PROJECT_NAME}/
)

# 安装include头文件
install(DIRECTORY include/
  DESTINATION include
)

# 添加lint测试
if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

# 导出依赖
ament_export_dependencies(
  rclcpp
  nav_msgs
  sensor_msgs
  geometry_msgs
  tf2_ros
  tf2_geometry_msgs
)

ament_package()